package cn.easybuy.dao.news;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.user.BaseDaoImpl;
import cn.easybuy.entity.News;

public class NewsDaoImpl extends BaseDaoImpl implements NewsDao {

	public NewsDaoImpl(Connection conn) {
		super(conn);
	}
	
	public News tableToClass(ResultSet rs) throws Exception  {
		News news = new News();
		news.setContent(rs.getString("content"));
		news.setCreateTime(rs.getDate("createTime"));
		news.setId(rs.getInt("id"));
		news.setTitle(rs.getString("title"));
		
		return news;
	}

	@Override
	public List<News> queryAllnews() {

		List<News> list = new ArrayList<News>();
		StringBuffer sql = new StringBuffer("SELECT id,title,content,createTime FROM easybuy_news");
		sql.append(" limit 0,5");
		ResultSet rs = null;
		try {
			rs = this.executeQuery(sql.toString(), null);
			
			while (rs.next()) {
				News news = tableToClass(rs);
				list.add(news);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeResource();
			this.closeResource(rs);
		}
		return list;
	}

}
